package fsu.cs.cis5930;

import java.util.ArrayList;
import java.util.List;

import fsu.cs.cis5930.ClientActivity.VictimInfo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ContactsDisplay extends Activity 
{
	private Connection mConnection;
	private GlobalApplication GLOBAL;
	
	private static final String TAG = "ContactsDisplay";
	
	private VictimInfo victim;
	
	private TextView contactTv;
	
	private ListView contactLv;
	private ContactAdapter contactAdapter;
	private Handler contactHandler;
	
	private static final int LAYOUT_CONTACT_LIST_ITEM = R.layout.display_contact_list_item;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.display_contact);
		
		GLOBAL = (GlobalApplication) getApplicationContext();
		mConnection = GLOBAL.getConnection();
		
		victim = GLOBAL.getVictim();
		
		contactTv = (TextView) findViewById(R.id.display_contact_tv);
		
		contactTv.setText("Contacts for user " + victim.MAC);
		
		contactLv = (ListView) findViewById(R.id.display_contact_lv);
		
		if (contactLv == null)
		{
			Log.e(TAG, "Could not find ListView");
			finish();
		}
		
		contactAdapter = new ContactAdapter(this, new ArrayList<Exploits.StolenContactInfo>());
		
		contactHandler = new Handler()
		{ 
			@Override
			public void handleMessage(Message msg)
			{ 
				Exploits.StolenContactInfo contact = (Exploits.StolenContactInfo) msg.obj;
				contactAdapter.addItem(contact);
			}
		};
		
		GLOBAL.setContactHandler(contactHandler);
		
		contactLv.setAdapter(contactAdapter);
		
		requestContacts();
	}
	
	private void requestContacts()
	{
		Log.v(TAG, "Sending request to server to get contacts");
		if (mConnection != null && victim != null)
		{
			Log.v(TAG, "Asking for contacts for victim " + victim.MAC);
			mConnection.sendStringToServer("LISTCONTACTS " + victim.MAC);
		}
	}
	
	public class ContactAdapter extends BaseAdapter
	{
		private Context mContext;
		private ArrayList<Exploits.StolenContactInfo> mList;



		public ContactAdapter(Context context, ArrayList<Exploits.StolenContactInfo> myList) 
		{
			this.mContext = context;
			this.mList = myList;
		}

		public int getCount() {
			return mList.size();
		}

		public Exploits.StolenContactInfo getItem(int position) {
			return mList.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public void addItem(Exploits.StolenContactInfo c)
		{
			mList.add(c);
			notifyDataSetChanged();
		}

		public void removeItem(VictimInfo c)
		{
			mList.remove(c);
			notifyDataSetChanged();
		}

		public View getView(int position, View convertView, ViewGroup viewGroup) {
			Exploits.StolenContactInfo entry = mList.get(position);
			
			if (convertView == null) 
			{
				LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(LAYOUT_CONTACT_LIST_ITEM, null);
			}
			
			TextView contactName = (TextView) convertView.findViewById(R.id.display_contact_list_item_name_tv);
			contactName.setText(entry.Name);


			TextView contactPhones = (TextView) convertView.findViewById(R.id.display_contact_list_item_phone_tv);
			
			ArrayList<String> phones = entry.phoneNumbers;
			
			if (phones.isEmpty())
			{
				contactPhones.setText("No phone numbers for this contact.");
			}
			
			else
			{
				String text = "";
				
				for (int i = 0; i < phones.size(); i++)
				{
					text += phones.get(i) + "\n";
				}
				
				contactPhones.setText(text);
			}

			TextView contactEmails = (TextView) convertView.findViewById(R.id.display_contact_list_item_email_tv);

			ArrayList<String> emails = entry.emailAddresses;
			
			if (emails.isEmpty())
			{
				contactEmails.setText("No email addresses for this contact.");
			}
			
			else
			{
				String text = "";
				
				for (int i = 0; i < emails.size(); i++)
				{
					text += emails.get(i) + "\n";
				}
				
				contactEmails.setText(text);
			}


			return convertView;

		}
	}
}
